package com.lin.chapter8;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.channel.socket.oio.OioSocketChannel;

import java.net.InetSocketAddress;

public class ClientOIOBootStrapTeat {
	public static void main(String[] args) {
		EventLoopGroup eventLoopGroup = new OioEventLoopGroup();
		Bootstrap bootstrap = new Bootstrap();
		bootstrap.group(eventLoopGroup)
				.channel(OioSocketChannel.class)
				.handler(new SimpleChannelInboundHandler<ByteBuf>() {
					@Override
					protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
						System.out.println("Received data");
					}
				});
		ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("www.baidu.com", 80));
		channelFuture.addListener((ChannelFutureListener) channelFuture1 -> {
			if (channelFuture1.isSuccess())
				System.out.println("Connection established");
			else{
				System.out.println("Connection attempt failed");
				channelFuture1.cause().printStackTrace();
			}
		});
	}
}
